<?php

namespace App\Console\Commands;

use App\Patent;
use App\PatentTemp;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;

class PatentTempCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:patentTemp {type?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(): int
    {
        $type = $this->argument('type');
        if ($type == 'new') {
            $this->batchAddTempByNew();
        } elseif ($type == 'sync') {
            $this->batchSyncFees();
        } elseif ($type == 'delete') {
            $this->batchDeletePatentTemp();
        } else {
            $type = 'all';
            $this->batchAddTempByFee();
        }
        echo $type . '-ok';
        return 0;
    }

    protected function batchDeletePatentTemp()
    {
        PatentTemp::query()->delete();
    }

    protected function batchAddTempByNew()
    {
        $count = 0;
        Patent::query()->with(['temp'])->has('fees', 0)
            ->chunk(500, function ($patents) use (&$count) {
                $data = $patents->filter(function ($item) {
                    return !$item->temp;
                })->pluck('patent_sn', 'patent_sn')->map(function ($patent_sn) {
                    return ['patent_sn' => $patent_sn];
                })->values()->all();
                DB::table('patent_temps')->insert($data);
                $count += count($data);
            });
        echo '数量---' . $count . "\n";
    }

    protected function batchAddTempByFee()
    {
        $count = 0;
        Patent::query()->with(['temp'])->has('fees', 0)
            ->orWhereHas('fees', function (Builder $query) {
                $query->where(function (Builder $query) {
                    $query->where('state', 0)
                        ->where('last_time', '<', Carbon::now()->addDays(30));
                })->orWhere(function (Builder $query) {
                    $query->where('state', 1)->whereNull('pay_time');
                });
            })->chunk(500, function ($patents) use (&$count) {
                $data = $patents->filter(function ($item) {
                    return !$item->temp;
                })->pluck('patent_sn', 'patent_sn')->map(function ($patent_sn) {
                    return ['patent_sn' => $patent_sn];
                })->values()->all();
                DB::table('patent_temps')->insert($data);
                $count += count($data);
            });
        echo '数量---' . $count . "\n";
    }

    //采集完后 批量同步专利
    protected function batchSyncFees()
    {
        $count = 0;
        PatentTemp::query()->with('patents')
            ->where('state', 2)
            ->chunk(20, function ($temps) use (&$count) {
                foreach ($temps as $temp) {
                    $patents = $temp->patents;
                    foreach ($patents as $model) {
                        $this->syncFees($temp, $model);
                        $count++;
                        echo $model['patent_sn'] . '---' . $count . "\n";
                    }
                }
            });
    }

    private function syncFees(PatentTemp $temp, Patent $patent)
    {
        $data = $temp->fees();
        if ($data) {
            $patent->fees()->delete();
            $patent->fees()->createMany($data);
        }
    }

}
